package com.demo.rabbitmq.confirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author sc
 * Created on  2018/9/16
 */
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory cf = new ConnectionFactory();
        cf.setVirtualHost("/");
        cf.setHost("localhost");
        cf.setPort(5672);
        cf.setNetworkRecoveryInterval(3000);
        cf.setAutomaticRecoveryEnabled(true);

        Connection connection = cf.newConnection();
        Channel channel = connection.createChannel();
        channel.confirmSelect();
        channel.addConfirmListener(new MyConfirmListener());

        String exchange = "test_confirm";
        String routingKey = "confrim.save";
        String queue_name = "confirm_queue";

        channel.exchangeDeclare(exchange,"topic",true);
        channel.queueDeclare(queue_name,false,false,false,null);
        channel.queueBind(queue_name,exchange,routingKey);

        QueueingConsumer qc = new QueueingConsumer(channel);

        channel.basicConsume(queue_name,true,qc);


        while (true){
            QueueingConsumer.Delivery delivery = qc.nextDelivery();
            System.out.println(delivery.getBody());
        }

    }
}
